package cn.tedu.boot22.controller;

import cn.tedu.boot22.utils.DBUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Controller
public class EmpController {
    @RequestMapping("/select")
    @ResponseBody
    public String select(){
        String html="<center>\n" +
                "    <table border=\"1px\">\n" +
                "        <caption>员工列表</caption>\n" +
                "        <tr>\n" +
                "            <th>id</th>\n" +
                "            <th>名字</th>\n" +
                "            <th>工资</th>\n" +
                "        </tr>\n";
        //插入数据库数据
        try (Connection conn = DBUtils.getConn()) {
            String sql = "select id,name,sal from emp";
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery(sql);
            //遍历每条数据
            while (rs.next()){
                int id = rs.getInt(1);
                String name = rs.getString(2);
                double sal = rs.getDouble(3);
                html += "        <tr>\n" +
                        "            <td>"+id+"</td>\n" +
                        "            <td>"+name+"</td>\n" +
                        "            <td>"+sal+"</td>\n" +
                        "        </tr>\n";
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


        html += "    </table>\n" +
                "</center>";
//        System.out.println(html);
        return html;
    }
}
